package com.kgis.map.service.hessian {
    import com.kgis.map.Map;
    import com.kgis.map.feature.Feature;
    import com.kgis.map.geometry.basetype.Extent;
    import com.kgis.map.layer.FeatureLayer;
    import com.kgis.map.layer.LayerData;
    import hessian.client.HessianService;
    import mx.controls.Alert;
    import mx.rpc.AsyncResponder;
    import mx.rpc.AsyncToken;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;

    public class SpatialDataManageService {
        private static var _userService:HessianService;
        private var _map:Map = null;

        protected static function getUserService():HessianService {
            if (!_userService) {
                //_userService=new HessianService("http://192.168.0.221:8000/pkmap/hessian/spatialDataManagementService");
                //   _userService=new HessianService("http://127.0.0.1:8000/pkmap/hessian/spatialDataManagementService");	
                //_userService.destination = "http://127.0.0.1:8000/pkmap/hessian/spatialDataManagementService";
                //_userService.destination = "http://192.168.0.221:8000/pkmap/hessian/spatialDataManagementService"
                //_userService.showBusyCursor=true;
            }
            return _userService;
        }

        public function SpatialDataManageService(map:Map) {
            this._map = map;
        }

        /**根据图层id和地图范围获取 矢量设施数据*/
        public function getGeometryByLayerAndRange(layerid:int, extent:Extent):void {
            return;
            var token:AsyncToken = getUserService().getOperation("getGeometryByLayerAndRange").send(layerid, extent);
            token.addResponder(new AsyncResponder(getGeometryByLayerAndRangeResult, getGeometryByLayerAndRangeFault));
        }

        public function getGeometryByLayerAndRangeResult(event:ResultEvent, token:Object = null):void {
            //加载矢量数据到相应的featurelayer
            var layerData:LayerData = event.result as LayerData;

            var layer:FeatureLayer = this.map.getLayerById(layerData.layerId) as FeatureLayer;
            layer.reset();

            var features:Vector.<Feature> = new Vector.<Feature>();
            var nCount:int = layerData.features.length;
            for (var i:int = 0; i < nCount; i++) {
                features.push(layerData.features.getItemAt(i));
            }
            layer.addFeatures(features);
            layer.redraw();
        }

        public function getGeometryByLayerAndRangeFault(event:FaultEvent, token:Object = null):void {
            Alert.show("根据extent获取图层数据错误！");
        }

        public function get map():Map {
            return _map;
        }

        public function set map(value:Map):void {
            _map = value;
        }
    }
}